//
//  YFSearchView.m
//  project
//
//  Created by yefei on 2019/5/5.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFSearchView.h"
#import "YFMapSearchResultCell.h"


@interface YFSearchView () <AMapSearchDelegate, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *searchShadowView;
@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UITableView *resultTableView;

@property (nonatomic, strong) NSArray<AMapPOI *> *resultArray;
@property (nonatomic, strong) AMapSearchAPI *searchApi;


@end

@implementation YFSearchView

+ (YFSearchView *)searchView {
    YFSearchView *view = [[UINib nibWithNibName:@"YFSearchView" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
    view.frame = UIScreen.mainScreen.bounds;
    return view;
}


- (void)showOn:(UIView *)view {
    self.alpha = 0;
    [view addSubview:self];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.alpha = 1;
    }];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.searchShadowView.layer.cornerRadius = 10;
    self.searchShadowView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.searchShadowView.layer.shadowOffset = CGSizeMake(0, 2);
    self.searchShadowView.layer.shadowOpacity = 0.05;
    self.searchShadowView.layer.shadowRadius = 5;
    
    self.resultTableView.layer.cornerRadius = 10;
    self.resultTableView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.resultTableView.layer.shadowOffset = CGSizeMake(0, 2);
    self.resultTableView.layer.shadowOpacity = 0.05;
    self.resultTableView.layer.shadowRadius = 5;
    YFEmptyView *emptyView = [YFEmptyView view];
    emptyView.message = @"暂无数据";
    self.resultTableView.emptyViewOffset = -50;
    self.resultTableView.emptyView = emptyView;
    
    self.resultTableView.rowHeight = 55;
    self.resultTableView.dataSource = self;
    self.resultTableView.delegate = self;
    self.resultTableView.tableFooterView = UIView.new;
    self.resultTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.resultTableView registerNib:[UINib nibWithNibName:@"YFMapSearchResultCell" bundle:nil] forCellReuseIdentifier:@"YFMapSearchResultCell"];
    
    @weakify(self)
    [[self.cancelButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dismiss];
    }];
    
    self.searchTextField.delegate = self;
    [self.searchTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
        request.keywords = x;
        request.requireExtension = YES;
        request.requireSubPOIs = YES;
        request.cityLimit = YES;
        request.page = 1;
        request.offset = 50;
        request.sortrule = 0;
        [self.searchApi AMapPOIKeywordsSearch:request];
    }];
    [self.searchTextField becomeFirstResponder];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.keywords = textField.text;
    request.requireExtension = YES;
    request.requireSubPOIs = YES;
    request.cityLimit = YES;
    request.page = 1;
    request.offset = 50;
    request.sortrule = 0;
    [self.searchApi AMapPOIKeywordsSearch:request];
    return YES;
}

#pragma mark - AMapSearchDelegate
-(void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response {
    if (response.pois.count == 0) {
        self.resultArray = NSArray.array;
    } else {
        self.resultArray = response.pois;
    }
    [self.resultTableView reloadData];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFMapSearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFMapSearchResultCell" forIndexPath:indexPath];
    cell.poi = self.resultArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_resultAction) {
        _resultAction(self.resultArray[indexPath.row]);
    }
    [self dismiss];
}

#pragma mark - Lazy load
- (NSArray<AMapPOI *> *)resultArray {
    if (!_resultArray) {
        _resultArray = NSArray.array;
    }
    return _resultArray;
}

- (AMapSearchAPI *)searchApi {
    if (!_searchApi) {
        _searchApi = [[AMapSearchAPI alloc] init];
        _searchApi.delegate = self;
    }
    return _searchApi;
}

@end
